// Employee.m
//
// Created on Thu Sep 15 17:31:19 PDT 1994 by NeXT EOModeler.app Version 59

#import "Employee.h"
#import "Department.h"
#import "ValidatingDelegate.h"

@implementation Employee

// Accessors
- (double)salary {
     return salary;
}


// Key Validation 

- (NSArray *)keysToValidate
{
    return [NSArray arrayWithObjects:@"salary", @"city", @"address", nil];
}

- (NSString *)validateSalary:(NSNumber *)proposed
{
    if ([proposed doubleValue] < 1000.0)
        return @"Salaries must be greater than $1,000";

    return  nil; // no error
}

- (NSString *)validateCity:(NSString *)proposed
{
    if ([proposed length] == 0)
        return @"You must enter a city";
    return nil;
}

- (NSString *)validateAddress:(NSString *)proposed
{
    if ([proposed length] > 40)
        return @"Addresses may not be longer than 40 characters";
    return nil;
}

// Final validation ////
- (NSDictionary *)validForDataSource:(id <EODataSources>)dataSource
{
    // By default this message is called for both insertion and update.
    // The superclass implementation will call our per-key validators (above).
    // We can also check any broader validation constraints.  E.g. is salary+bonus
    // at an exceptable level?  In this case we message a related object (department)
    // to see if it's okay with our settings.  Department, in turn, check if our
    // salary is within the department limits.
    NSString *errorMessage;
    const NSString *generalKey = @"General";
    NSDictionary *errors;

    if (errors = [super validForDataSource:dataSource])
        return errors;
 
    // check cross property validation constraints
    // Ask our department if it's okay with our new values
    if (errorMessage = [toDepartment validateEmployee:self])
        return [NSDictionary dictionaryWithObjects:&errorMessage forKeys:&generalKey count:1];

    return nil;
}

- (void)dealloc
{
    [address autorelease];
    [city autorelease];
    [firstName autorelease];
    [lastName autorelease];
    [toDepartment dealloc];
    [super dealloc];
}


@end
